﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PBOffice.DAL;
using PBOffice.BLL;

public partial class NewsManageModule_viewNews : System.Web.UI.Page
{
    NewsManager nm = new NewsManager();
    List<v_NewsInfo> newsList = new List<v_NewsInfo>();
    List<v_NewsInfo> newsPage = new List<v_NewsInfo>();
    int currentPage;
    int pageSize = 2;
    int newsCount;  //新闻总条数
    protected void Page_Load(object sender, EventArgs e)
    {
        //页面首次加载
        if(!IsPostBack)
        {
            List<NewsType> newsTypeList = new List<NewsType>();
            newsTypeList = nm.ShowNewsType();
            NewsType item=new NewsType();
            item.NewsTypeId=" "; 
            newsTypeList.Insert(0,item);
            ddlNewsType.DataSource = newsTypeList;   //绑定新闻类型
            ddlNewsType.DataTextField = "NewsTypeId";
            ddlNewsType.DataValueField = "NewsTypeNo";
            ddlNewsType.DataBind();
            newsList = nm.ShowAllNewsInfo();    //查询所有新闻信息
            currentPage = 1;             //默认显示第一页
            lblPageIndex.Text = currentPage.ToString();     //当前页码：第一页
            ShowNewsPage(currentPage);              //显示新闻信息  
            int pageIndex=Convert.ToInt32( lblPageIndex.Text);
            int pageCount=Convert.ToInt32( lblPageCount.Text);
            if (pageIndex == 1)
                btnPre.Visible = false;     //隐藏上一页按钮
            if (pageIndex == pageCount)
                btnNext.Visible = false;     //隐藏下一页按钮
        }
    }
    //显示指定页面的新闻信息
    private void ShowNewsPage(int currentPage)
    {
        int pageIndex = currentPage - 1;
        newsCount = newsList.Count();            //新闻总条数  
        if (newsCount % pageSize == 0)
            lblPageCount.Text = (newsCount / pageSize).ToString();
        else
            lblPageCount.Text = (Convert.ToInt32(newsCount / pageSize) + 1).ToString();
        int newslastPageCount = newsCount - pageIndex * pageSize;
        if (newslastPageCount < pageSize)
        {
            for (int i = pageIndex * pageSize; i < newsCount; i++)
            {
                newsPage.Add(newsList[i]);
            }  
        }
        else
        {
            for (int i = pageIndex * pageSize; i < pageIndex * pageSize + pageSize; i++)
            {
                newsPage.Add(newsList[i]);
            }           
        }
        rptNews.DataSource = newsPage;               //绑定新闻信息
        rptNews.DataBind(); 

    }
    //查询按钮单击事件
    protected void btnNewsSearch_Click(object sender, EventArgs e)
    {
        ShowSearchNews();     //根据标题和类型联合查询新闻信息
        currentPage = 1;             //默认显示第一页
        lblPageIndex.Text = currentPage.ToString();     //当前页码
        ShowNewsPage(currentPage);              //显示新闻信息
    }
    //根据标题和类型联合查询新闻信息
    private void ShowSearchNews()
    {
        string newsTitle = txtNewsTitle.Text.Trim();
        int newsTypeNo = Convert.ToInt32(ddlNewsType.SelectedValue.ToString());
        if (ddlNewsType.SelectedIndex == 0)
            newsList = nm.ShowNewsInfoByNewsTitle(newsTitle);          //新闻类型为空时，根据新闻标题模糊查询新闻信息
        else
            newsList = nm.ShowNewsInfoByNewsTypeAndNewsTitle(newsTypeNo, newsTitle);   //根据新闻标题和新闻类型查询新闻信息
    }
    //上一页按钮单击事件
    protected void btnPre_Click(object sender, EventArgs e)
    {
        ShowSearchNews();     //根据标题和类型联合查询新闻信息
        int currentPage = Convert.ToInt32(lblPageIndex.Text);
        int pageCount=Convert.ToInt32( lblPageCount.Text);   //总页数
        if (currentPage > 1)
        {
            currentPage = currentPage - 1;
            lblPageIndex.Text = currentPage.ToString();          
        }
        ShowNewsPage(currentPage);              //显示新闻信息 
        ShowButton(pageCount, currentPage);     //上一页、下一页是否显示
    }
    //下一页按钮单击事件
    protected void btnNext_Click(object sender, EventArgs e)
    {
        ShowSearchNews();     //根据标题和类型联合查询新闻信息
        int pageCount=Convert.ToInt32( lblPageCount.Text);   //总页数
        int currentPage = Convert.ToInt32(lblPageIndex.Text);
        if (currentPage < pageCount)
        {
            currentPage = currentPage + 1;
            lblPageIndex.Text = currentPage.ToString();           
        }
        ShowNewsPage(currentPage);              //显示新闻信息 
        ShowButton(pageCount, currentPage);     //上一页、下一页是否显示
    }
    //上一页、下一页是否显示
    private void ShowButton(int pageCount, int currentPage)
    {
        if (currentPage > 1 && currentPage < pageCount)
        {
            btnPre.Visible = true;
            btnNext.Visible = true;
        }
        else if (currentPage == 1 && currentPage<pageCount)
        {
            btnPre.Visible = false;
            btnNext.Visible = true;
        }
        else if (currentPage == 1 && pageCount > 1)
        {
            btnNext.Visible = true;
            btnPre.Visible = false;
        }
        else if (currentPage > 1 && currentPage == pageCount)
        {
            btnPre.Visible = true;
            btnNext.Visible = false;
        }
    }
}